﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;
namespace QldtSdh.GUI.HeThong
{
    public partial class FormPhanQuyen : QldtSdh.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        List<ToolStripMenuItem> myItems;
        MenuStrip menu;
        public FormPhanQuyen(MenuStrip menuStrip)
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            allowUpdate = false;
            menu = menuStrip;
            InitGrid();
            LoadNguoiSuDung();
            LoadGrid();
            allowUpdate = true;
        }

        void LoadNguoiSuDung()
        {
            cmbNguoiSuDung.DataSource = BLL.HeThong.GetList(true);
            cmbNguoiSuDung.DisplayMember = "FullName";
            cmbNguoiSuDung.ValueMember = "UserName";
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colChoPhep.DataPropertyName = "Allow";
            colChucNang.DataPropertyName = "FunctionName";
        }

        void LoadGrid()
        {
            if (cmbNguoiSuDung.SelectedIndex != -1)
            {
                myItems = BizPhanQuyen.GetItems(this.menu);
                var userName = cmbNguoiSuDung.SelectedValue.ToString();
                var lsUser_Function = BizPhanQuyen.GetListByUser(userName);
                List<FunctionItem> ls = new List<FunctionItem>();
                int i = 0;
                foreach (var item in myItems)
                {
                    i++;
                    if (i < 10)
                        continue;
                    string more = "          ";
                    if (item.Text == "Nghiên cứu sinh")
                    {
                        more = "";
                    }
                    if (item.Text == "Trợ giúp")
                        break;
                    if (item.DropDown.Items.Count > 0)
                    {
                        more = "";
                    }
                    if (lsUser_Function.Contains(item.Text))
                        ls.Add(new FunctionItem { FunctionName = more + item.Text, Allow = true });
                    else
                        ls.Add(new FunctionItem { FunctionName = more + item.Text, Allow = false });

                }
                bdData.DataSource = ls;
                gridData.DataSource = bdData;
            }
        }
        class FunctionItem
        {
            public string FunctionName { get; set; }
            public bool Allow { get; set; }
        }

        private void gridData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == colChoPhep.Index)
                {
                    if (myItems != null && myItems.Count > 0)
                    {
                        var current = bdData.Current as FunctionItem;
                        BizPhanQuyen.AddOrDelete(cmbNguoiSuDung.SelectedValue.ToString(), current.FunctionName.Trim());
                    }
                }
            }
        }


        private void cmbNguoiSuDung_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadGrid();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            var ls = bdData.DataSource as List<FunctionItem>;
            if (ls != null && ls.Count > 0)
            {
                var ck = ls[0].Allow;
                foreach (var item in ls)
                {
                    item.Allow = !ck;
                    BizPhanQuyen.AddOrDelete(cmbNguoiSuDung.SelectedValue.ToString(), item.FunctionName.Trim());
                }
            }
            gridData.Refresh();
        }
    }
}
